﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace WCF.Common.DAL
{
    public class ServiceObjectDAL : BaseDAL
    {
        /// <summary>获取应用表数据</summary>
        /// <returns>List</returns>
        public List<ServiceObject> Gets()
        {
            var context = GetDataContext();
            var result = context.ServiceObject.OrderBy(p => p.Order).ToList();
            return result;
        }
        /// <summary>根据编码查找应用</summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ServiceObject SeachByCode(string id)
        {
            var context = Gets();
            if (context.Any(p => p.ID == id))
                return context.First(p => p.ID == id);
            else
                return new ServiceObject();
        }
        /// <summary>增加数据</summary>
        /// <param name="Info"></param>
        /// <returns>OperateResult</returns>
        public OperateResult Insert(ServiceObject Info)
        {
            try
            {
                var context = GetDataContext();
                context.ServiceObject.InsertOnSubmit(Info);
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
            return new OperateResult() { IsSuccess = true };
        }
        /// <summary>删除应用，如果有角色关联关系,不能删除</summary>
        /// <param name="id"></param>
        /// <returns>OperateResult</returns>
        public OperateResult Delete(string id)
        {
            OperateResult result = new OperateResult();
            try
            {
                var context = GetDataContext();
                var appcfg = context.ServiceObject.Single(p => p.ID == id);
                if (appcfg != null)
                {
                    context.ServiceObject.DeleteOnSubmit(appcfg);
                    context.SubmitChanges();
                    result.IsSuccess = true;
                }
                else
                {
                    result.IsSuccess = false;
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>修改数据</summary>
        /// <param name="Info"></param>
        /// <returns>OperateResult</returns>
        public OperateResult Update(ServiceObject Info)
        {
            try
            {
                var context = GetDataContext();
                var obj = context.ServiceObject.Single(p => p.ID == Info.ID);
                obj.Args = Info.Args;
                obj.Bgc = Info.Bgc;
                obj.ID = Info.ID;
                obj.Caption = Info.Caption;
                obj.Descript = Info.Descript;
                obj.Frc = Info.Frc;
                obj.Icon1 = Info.Icon1;
                obj.Order = Info.Order;
                obj.Tags = Info.Tags;
                obj.Type = Info.Type;
                obj.URL = Info.URL;
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
            return new OperateResult() { IsSuccess = true };
        }

        public OperateResult SetAutoload(string[] ids)
        {
            try
            {
                string key = "autoload;";
                var context = this.GetDataContext();
                foreach (var id in ids)
                {
                    if (context.ServiceObject.Any(p => p.ID == id && p.Args.IndexOf(key) == -1))
                    {
                        context.ServiceObject.First(p => p.ID == id).Args += key;
                    }
                }
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
            return new OperateResult() { IsSuccess = true };
        }
        public OperateResult SetUnAutoload(string[] ids)
        {
            try
            {
                string key = "autoload;";
                var context = this.GetDataContext();
                foreach (var id in ids)
                {
                    if (context.ServiceObject.Any(p => p.ID == id && p.Args.IndexOf(key) >=0))
                    {
                        context.ServiceObject.First(p => p.ID == id).Args.Replace(key,string.Empty);
                    }
                }
                context.SubmitChanges();
            }
            catch (Exception ex)
            {
                return new OperateResult() { IsSuccess = false, Message = ex.Message };
            }
            return new OperateResult() { IsSuccess = true };
        }
    }
}